<?php

declare(strict_types=1);

namespace App\Robot\Amqp\Consumer;

use App\CustHub\Constants\ContactTaskCenterStatusConstants;
use App\CustHub\Constants\ContactTaskCenterTyperConstants;
use App\CustHub\Model\ChContact;
use App\CustHub\Model\ChContactTaskCenter;
use App\CustHub\Service\ChContactDedupService;
use App\CustHub\Service\ChContactService;
use App\CustHub\Service\ChSmartAutoPushService;
use App\CustHub\Service\LowQueueService;
use App\Robot\Dto\CommonJobDto;
use App\Robot\Job\CommonJob;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
use Hyperf\Coroutine\Coroutine;
use PhpAmqpLib\Message\AMQPMessage;
use function Hyperf\Coroutine\co;

#[Consumer(exchange: "common_exchange", routingKey: "common_exchange", queue: "common_exchange", name: "commonExchange", nums: 1)]
class CommonConsumer extends ConsumerMessage
{
    public function consumeMessage($data, AMQPMessage $message): Result
    {
        if (empty($data['classname']) || empty($data['method']) || empty($data['data'])) {
            return Result::ACK;
        }

        //延迟执行
        if (isset($data['delay']) && $data['delay'] > 0) {
            container()->get(LowQueueService::class)->push(new CommonJob(new CommonJobDto(
                classname: $data['classname'],
                method: $data['method'],
                data: $data['data']
            )),$data['delay']);
            return Result::ACK;
        }
        container()->get($data['classname'])->{$data['method']}($data['data']);
        return Result::ACK;
    }


    public function isEnable(): bool
    {
        return \Hyperf\Support\env('AMQP_CONSUMER_ENABLE', false);
    }
}